package com.yydream.controller;

import com.yydream.common.utils.R;
import com.yydream.common.valid.AddGroup;
import com.yydream.common.valid.UpdateGroup;
import com.yydream.dto.SysUserDTO;
import com.yydream.dto.SysUserPageParamsDTO;
import com.yydream.dto.UserRelationRoleDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.yydream.service.SysUserService;

import javax.validation.Valid;

/**
 * 用户表控制层
 * @author : yydream
 * @date : 2024-6-17
 */
@Api(tags = "用户管理功能接口")
@RestController
public class SysUserController{
    @Autowired
    private SysUserService sysUserService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @ApiOperation("通过ID查询单条数据")
    @GetMapping("/sysUser/detail")
    public R queryById(@RequestParam(value = "id") Long id){
        return sysUserService.queryById(id);
    }

    /**
     * 分页查询
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @PostMapping("/sysUser/list")
    public R list(@RequestBody SysUserPageParamsDTO req){
        return sysUserService.listPage(req);
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增数据")
    @PostMapping("/sysUser/insert")
    public R add(@Validated(AddGroup.class) @RequestBody SysUserDTO sysUser){
        return sysUserService.insert(sysUser);
    }

    /**
     * 更新数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @ApiOperation("更新数据")
    @PostMapping("/sysUser/update")
    public R edit(@Validated(UpdateGroup.class) @RequestBody SysUserDTO sysUser){
        return sysUserService.update(sysUser);
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @PostMapping("/sysUser/deleteById")
    public R deleteById(@RequestParam(value = "ids") Long[] ids){
        return sysUserService.deleteById(ids);
    }
    /**
     * 用户关联角色
     *
     * @param userRelationRoleDTO 实例对象
     * @return 实例对象
     */
    @ApiOperation("用户关联角色")
    @PostMapping("/sysUser/userRelationRole")
    public R userRelationRole(@Valid @RequestBody UserRelationRoleDTO userRelationRoleDTO){
        return sysUserService.userRelationRole(userRelationRoleDTO);
    }

    /**
     * 查询角色已关联角色
     *
     * @param userId 角色id
     * @return 实例对象
     */
    @ApiOperation("查询角色已关联角色")
    @GetMapping("/sysRole/userRelationRole/{userId}")
    public R getRoleRelationMenu(@PathVariable(name = "userId") Integer userId){
        return sysUserService.userRelationRoleByUserId(userId);
    }


    /**
     * 获取当前登录人的全部信息
     *
     * @return 实例对象
     */
    @ApiOperation("获取当前登录人的全部信息")
    @GetMapping("/sysUser/userInfo")
    public R getRoleRelationMenu(){
        return sysUserService.userInfo();
    }

}